﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "WallTagPositionPoints.h"
#include "DbObjectUtils.h"
#include "IRegeneratorDataIdCreator.h"
#include "IElementBasicInformation.h"
#include "IDocument.h"
#include "IRegenerator.h"
#include "IGenericElement.h"
#include "WallTag.h"
#include "UiDocumentViewUtils.h"
#include "IElementModelShape.h"
#include "IDocument.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

using namespace gcmp;
using namespace Sample;



DBOBJECT_DATA_DEFINE(WallTagPositionPoints)
{
    SetOwnerElement(nullptr);
}

WallTag* WallTagPositionPoints::GetWallTag()
{
    IGenericElement* pGenericElement = quick_cast<IGenericElement>(GetOwnerElement());
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pGenericElement, L"GenericElement不能为空",L"GDMP",L"2023-12-30");
    WallTag* pWallTag = quick_cast<WallTag>(pGenericElement->GetExternalObject());
    return pWallTag;
}

const WallTag* WallTagPositionPoints::GetWallTag() const
{
    const IGenericElement* pGenericElement = quick_cast<IGenericElement>(GetOwnerElement());
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pGenericElement, L"GenericElement不能为空",L"GDMP",L"2023-12-30");
    const WallTag* pWallTag = quick_cast<WallTag>(pGenericElement->GetExternalObject());
    return pWallTag;
}

bool WallTagPositionPoints::SetOwnerElement(IElement* pOwnerElement)
{
    m_pOwnerElement = pOwnerElement;
    return true;
}

IElement* WallTagPositionPoints::GetOwnerElement()
{
    return m_pOwnerElement.Get();
}


const IElement* WallTagPositionPoints::GetOwnerElement() const
{
    return m_pOwnerElement.Get();
}

int WallTagPositionPoints::GetControlPointCount() const
{
    return 1;
}

gcmp::Vector3d WallTagPositionPoints::GetControlPoint(int index) const
{
    DBG_WARN_AND_RETURN_UNLESS(index < GetControlPointCount(), Vector3d::Zero, L"索引不能超过控制点个数",L"GDMP",L"2023-12-30");
    const WallTag* pWallTag = GetWallTag();
    DBG_WARN_AND_RETURN_UNLESS(pWallTag, Vector3d::Zero, L"WallTag获取失败",L"GDMP",L"2023-12-30");
    Vector3d point = pWallTag->GetReferenceLineStartPoint() + pWallTag->GetReferenceLineDir();
    return point;
}

void WallTagPositionPoints::SetControlPoint(int index, const Vector3d& point)
{
    DBG_WARN_AND_RETURN_VOID_UNLESS(index < GetControlPointCount(), L"索引不能超过控制点个数",L"GDMP",L"2023-12-30");
    WallTag* pWallTag = GetWallTag();
    DBG_WARN_AND_RETURN_VOID_UNLESS(pWallTag, L"WallTag获取失败",L"GDMP",L"2023-12-30");
    if (index == 0)
    {
        Vector3d referenceStartPoint = pWallTag->GetReferenceLineStartPoint();
        pWallTag->SetReferenceLineDir(point - referenceStartPoint);
    }
    pWallTag->MarkReferenceLineDirRdId();

    IGenericElement* pGenericElement = quick_cast<IGenericElement>(GetOwnerElement());
    DBG_WARN_AND_RETURN_VOID_UNLESS(pGenericElement, L"GenericElement获取失败",L"GDMP",L"2023-12-30");
    if (const IElementPositionPoints* pElementPositionPoints = pGenericElement->GetPositionPoints())
    {
        for (int i = 0; i < pElementPositionPoints->GetControlPointCount(); ++i)
        {
            MarkControlPointRdId(i);
        }
    }
}

RegenDataId WallTagPositionPoints::GetControlPointRdId(int index) const
{
    static int dataId = IRegeneratorDataIdCreator::CreateRegenDataId(
        WSTRING_OF("WallTagPositionPoints"),
        WSTRING_OF("Sample" + "WallTagPositionPoints"));
    const IElement* pOwnerElement = GetOwnerElement();
    DBG_WARN_AND_RETURN_UNLESS(pOwnerElement, RegenDataId(), L"Element获取失败",L"GDMP",L"2023-12-30");
    const IElementBasicInformation* pElementBasicInformation = pOwnerElement->GetBasicInformation();
    DBG_WARN_AND_RETURN_UNLESS(pElementBasicInformation, RegenDataId(), L"ElementBasicInformation获取失败",L"GDMP",L"2023-12-30");
    return RegenDataId(pElementBasicInformation->GetElementId().AsInt64(), dataId, index);
}

void WallTagPositionPoints::MarkControlPointRdId(int index)
{
    const IElement* pOwnerElement = GetOwnerElement();
    DBG_WARN_AND_RETURN_VOID_UNLESS(pOwnerElement, L"Element获取失败",L"GDMP",L"2023-12-30");
    IDocument* pDocument = pOwnerElement->GetDocument();
    DBG_WARN_AND_RETURN_VOID_UNLESS(pDocument, L"Document获取失败",L"GDMP",L"2023-12-30");
    IRegenerator* pRegenerator = pDocument->GetRegenerator();
    DBG_WARN_AND_RETURN_VOID_UNLESS(pRegenerator, L"Regenerator获取失败",L"GDMP",L"2023-12-30");
    pRegenerator->MarkRegenDataId(GetControlPointRdId(index));
}
